package com.yachang.qym.usbhost.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.leakcanary.RefWatcher;
import com.yachang.qym.usbhost.R;
import com.yachang.qym.usbhost.UsbApplication;
import com.yachang.qym.usbhost.activity.RouteActivity;
import com.yachang.qym.usbhost.model.AreaModel;
import com.yachang.qym.usbhost.model.RouteModel;
import com.yachang.qym.usbhost.utils.AllData;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by qym on 2015/12/21.
 */
public class RouteFragment extends Fragment {

    private View mParentView ;
    private LinearLayout mLvRoute;
    //private RouteAdapter mAdapter;
    private List<AreaModel> areaModels;
    private List<RouteModel> routeModels;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mParentView = inflater.inflate(R.layout.fragment_route, container, false);
        areaModels = AllData.areaModels;
        routeModels = AllData.routeModels;
        if(areaModels.size() == 0){
            Toast.makeText(getActivity(), "无区域信息!", Toast.LENGTH_SHORT).show();
        }else {
            mLvRoute = (LinearLayout) mParentView.findViewById(R.id.lv_route);
            if (areaModels.size() > 0) {
                for (int i = 0; i < areaModels.size(); i++) {
                    mLvRoute.addView(addHeaderView(i));
                    if (areaModels.get(i).getAreaLong() > 0) {
                        for (int j = 0; j < areaModels.get(i).getRoutes().size(); j++) {
                            mLvRoute.addView(addItemView(i, j));
                        }
                    }
                }
            }
        }
        return mParentView;
    }

    private View addHeaderView(int areaID){
        View header = LayoutInflater.from(getActivity()).inflate(R.layout.item_area_header, null, false);
        TextView headName = (TextView) header.findViewById(R.id.tv_area_name);
        headName.setText(areaModels.get(areaID).getAreaId());
        return header;
    }

    private View addItemView(int areaID, final int count){
        View item = LayoutInflater.from(getActivity()).inflate(R.layout.item_route, null, false);

        TextView tvSendName;
        TextView tvRouteName;
        TextView tvRouteStyle;
        TextView tvRouteOtherName;
        LinearLayout parent = (LinearLayout) item.findViewById(R.id.layout_parent);

        tvSendName = (TextView) item.findViewById(R.id.tv_caller_name);
        tvRouteName = (TextView) item.findViewById(R.id.tv_route_name);
        tvRouteStyle = (TextView) item.findViewById(R.id.tv_call_style);
        tvRouteOtherName = (TextView) item.findViewById(R.id.tv_route_other_name);

        final int routeID = areaModels.get(areaID).getRoutes().get(count).getID();
        tvRouteName.setText(routeModels.get(routeID).getName());
        tvSendName.setText(AllData.contactModels.get(routeModels.get(routeID).getCallerID()).getName());
        tvRouteOtherName.setText("DMR/CH" + (count + 1));
        int callStyle = AllData.contactModels.get(routeModels.get(routeID).getCallerID()).getStyle();
        switch (callStyle){
            case 0:tvRouteStyle.setText("组呼");break;
            case 1:tvRouteStyle.setText("个呼");break;
            case 2:tvRouteStyle.setText("全呼");break;
        }

        parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), RouteActivity.class);
                intent.putExtra("RouteID", routeID);
                intent.putExtra("DmrID", count);
                startActivity(intent);
            }
        });
        return item;
    }


    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = UsbApplication.getRefWatcher(getActivity());
        refWatcher.watch(this);
    }
}

